package com.google.gwt.mobile.widgets.utils;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.EventListener;

/**
 * The Class Utils.
 */
public final class Utils {

  private Utils() {
    // Utility class
  }

  /**
   * Sets the event listener.
   * 
   * @param el the el
   * @param event the event
   * @param capture the capture
   * @param listener the listener
   */
  public static native void setEventListener(Element el, String event, boolean capture,
      EventListener listener) /*-{
		var callBack = function(e) {
			ele.removeEventListener(event, callBack, capture);
			listener.@com.google.gwt.user.client.EventListener::onBrowserEvent(Lcom/google/gwt/user/client/Event;)(e);
		};
		el.addEventListener(event, callBack, capture);
  }-*/;

  /**
   * Adds the event listener.
   * 
   * @param el the el
   * @param event the event
   * @param capture the capture
   * @param listener the listener
   * @return the java script object
   */
  public static native JavaScriptObject addEventListener(Element el, String event, boolean capture,
      EventListener listener) /*-{
		var callBack = function(e) {
			listener.@com.google.gwt.user.client.EventListener::onBrowserEvent(Lcom/google/gwt/user/client/Event;)(e);
		};
		el.addEventListener(event, callBack, capture);
		return callBack;
  }-*/;

  /**
   * Removes the event listener.
   * 
   * @param el the el
   * @param event the event
   * @param capture the capture
   * @param callBack the call back
   */
  public static native void removeEventListener(Element el, String event, boolean capture,
      JavaScriptObject callBack) /*-{
		el.removeEventListener(event, callBack, capture);
  }-*/;

  /**
   * Checks if is html form control.
   * 
   * @param el the el
   * @return true, if is html form control
   */
  public static boolean isHtmlFormControl(final com.google.gwt.dom.client.Element el) {
    if (el == null) {
      return false;
    }
    String FormControls = "button input select textarea";
    String nodeName = el.getNodeName().toLowerCase();
    return FormControls.contains(nodeName) || isHtmlFormControl(el.getParentElement());
  }

  public native static Element getActiveElement() /*-{
		return $doc.activeElement;
  }-*/;
}
